import viteBaseConfig from './vite.base.config.js'
import viteDevConfig from './src/config/vite.dev.js'
import viteBuildConfig from './src/config/vite.build.js'
import { defineConfig, loadEnv } from 'vite'

export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  if (mode === 'dev') {
    return {
      ...viteBaseConfig,
      ...viteDevConfig,
      // 开发服务器选项 https://cn.vitejs.dev/config/server-options
      server: {
        open: true,
        port: 5179,
        proxy: {
          '/proxy': {
            target: env.VITE_APP_API_BASEURL,
            changeOrigin: command === 'serve' && env.VITE_OPEN_PROXY === 'true',
            rewrite: (path) => path.replace(/\/proxy/, ''),
          },
        },
      },
    }
  } else {
    return {
      ...viteBaseConfig,
      ...viteBuildConfig,
    }
  }
})
